package com.carcenter.order.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.carcenter.common.constant.product.ProductConstant;
import com.carcenter.common.model.dto.ProductQueryDto;
import com.carcenter.common.model.dto.SaveProductDto;
import com.carcenter.common.model.vo.ProductVo;
import com.carcenter.common.page.MyPageData;
import com.carcenter.common.result.Result;
import com.carcenter.order.entity.Products;
import com.carcenter.order.entity.Specifications;
import com.carcenter.order.service.IProductsService;
import com.carcenter.order.service.ISpecificationsService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author car
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/products")
public class ProductsController {
    @Autowired
    private IProductsService productsService;
    @Autowired
    private ISpecificationsService specificationsService;
    /**
     * 商品列表展示
     * @param pageNum
     * @param pageSize
     * @param queryDto
     * @return
     */
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result getList(@PathVariable Integer pageNum,
                       @PathVariable Integer pageSize,
                       @RequestBody ProductQueryDto queryDto
                       ){
        IPage<ProductVo> page = productsService.getList(pageNum,pageSize,queryDto);
        MyPageData  data = new MyPageData(page.getTotal(),page.getRecords());
        return Result.ok(data);
    }

    /**
     * 新增
     * @param saveProductDto
     * @return
     */
    @PostMapping("/saveProduct")
    public Result saveProduct(@RequestBody SaveProductDto saveProductDto){
        return productsService.saveProduct(saveProductDto);
    }
    /**
     * 编辑
     * @param saveProductDto
     * @return
     */
    @PostMapping("/editProduct")
    public Result editProduct(@RequestBody SaveProductDto saveProductDto){
        return productsService.editProduct(saveProductDto);
    }

    /**
     * 修改库存
     * @param id
     * @param inventory
     * @return
     */
    @PostMapping("/modify/{id}/{inventory}")
    public Result updateInventory(@PathVariable Integer id,@PathVariable Integer inventory){
        LambdaUpdateWrapper<Products> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Products::getInventory,inventory).eq(Products::getId,id);
        productsService.update(updateWrapper);
        return Result.ok();
    }

    /**
     * 匹配车型
     * @param id
     * @return
     */
    @GetMapping("/match/{id}")
    public Result match(@PathVariable Integer id){
        return productsService.match(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        productsService.removeById(id);
        return Result.ok();
    }

    /**
     * 停售
     * @param id
     * @return
     */
    @PutMapping("/put/discontinued/{id}")
    public Result put(@PathVariable Integer id){
        LambdaUpdateWrapper<Products> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Products::getLifecycle, ProductConstant.NOT_SALE).eq(Products::getId,id);
        productsService.update(updateWrapper);
        return Result.ok();
    }

    /**
     * 获取商品
     * @param id
     * @return
     */
    @GetMapping("/getProduct/{id}")
    public Result getProduct(@PathVariable Integer id){
        Products products = productsService.getById(id);
        return Result.ok(products);
    }


    @GetMapping("/getProductAndSpec/{id}")
    public Result getProductAndSpec(@PathVariable Integer id){
        Products products = productsService.getById(id);
        Specifications specifications = specificationsService.getById(products.getSpecificationId());

        SaveProductDto saveProductDto = new SaveProductDto();


        saveProductDto.setId(products.getId());
        saveProductDto.setSpecificationId(specifications.getId());


        BeanUtil.copyProperties(products, saveProductDto, "id", "specificationId");
        BeanUtil.copyProperties(specifications, saveProductDto, "id");

        return Result.ok(saveProductDto);
    }

}
